﻿using Ivory;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using DefCore;
using DefCore.Cameras;
using DefCore.Mapping;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Defender.Framework;
using DefCore.Core;
using DefCore.Interfaces;
using Ivory.Interface;
using DefCore.Structures;
using Defender.Framework.Structures;
using Defender.Tools;
using DefCore.Actors;
using Defender.Actors;

namespace Defender.GameTypes
{
    public class LevelRunner : LevelBase
    {
        //Fields
        public static string Key = "~@TESTINGPIT@~";
        public static string LevelFileName = "";

        //ctor
        public LevelRunner()
            : base()
        {

        }

        //Methods
        protected override void LoadLevelFile()
        {
            DefenderLevelFile dlf = new DefenderLevelFile();
            dlf.Load(LevelFileName);
            dlf.CreateFromLoad(this);
        }
        protected override bool OnLoadingScreen()
        {
            SelectionEngine.Instance.SelectionClick += SelectionControlLeftClick; 
            return true;
        }
        protected override void LoadInterface(UserInterface gui)
        {

        }

        protected override void LevelRender(SpriteBatch batch, GameTime time)
        {

        }
        protected override void OnSolve(InputControl input, GameTime time)
        {
            if (input.IsNewKeyPress(Keys.E))
            {
                //Switch to building a Lantern
                if (Tool == EnumLevelToolsMode.BUILDINGTOOL)
                {
                    Tool = EnumLevelToolsMode.NOTOOL;
                    StructureControlUnit.SetBlueprint(null);
                }
                else
                {
                    Tool = EnumLevelToolsMode.BUILDINGTOOL;
                    StructureControlUnit.SetBlueprint(BlueprintFactory.Create<TorchBlueprint>());
                }
            }
            else if (input.IsNewKeyPress(Keys.R))
            {
                //Switch to building a bridge
                if (Tool == EnumLevelToolsMode.BUILDINGTOOL)
                {
                    Tool = EnumLevelToolsMode.NOTOOL;
                    StructureControlUnit.SetBlueprint(null);
                }
                else
                {
                    Tool = EnumLevelToolsMode.BUILDINGTOOL;
                    StructureControlUnit.SetBlueprint(BlueprintFactory.Create<WoodBridgeBlueprint>());
                }
            }
            else if (input.IsNewKeyPress(Keys.T))
            {
                //Spawn a Drone
                if (!(SelectionEngine.Instance.CurrentlyHovered is Cell))
                    return;

                Cell hoveredCell = SelectionEngine.Instance.CurrentlyHovered as Cell;
                DirectionalUnit.AddActor(ActorFactory.Create<Drone>(new DungeonPos(hoveredCell.MapPosition.CenterTile)));
            }
        }

        private void SelectionControlLeftClick(object sender, SelectionClickEventArgs e)
        {
            ISelectable selectable = e.Selectable;
            DungeonPos pos = e.Position;

            bool isCellBase = selectable is CellBase;
            bool isCell = isCellBase && selectable is Cell;

            CellBase cellBase = !isCellBase ? null : selectable as CellBase;
            Cell cell = !isCell ? null : selectable as Cell;

            //We click somewhere
            if (isCellBase)
            {

            }
            else
                SelectionEngine.Instance.ClearSelection();
        }
    }
}
